#ifndef _DLS_LIB_H
#define _DLS_LIB_H

#include "ioutils.h"

namespace Audio {
/*
32-bit Time Cents
where tc = Time Cents:
tc = log2(time[secs]) * 1200*65536
time[secs] = 2^(tc/(1200*65536))
32-bit Absolute Pitch Cents
where pc = Pitch Cents, f = linear frequency:
pc = (log2(f/440) * 1200 + 6900) * 65536
f = 2^((pc/65536 - 6900)/1200) * 440
16-bit Relative Pitch Cents
where pc = Pitch Cents, f = linear frequency, F = reference frequency:
pc = log2(f/F) * 1200
f = 2^(pc/(1200)) * F
32-bit Relative Gain
where cb = Centibels, v = voltage, V = reference voltage:
cb = log10(v/V) * 200 * 65536
v = 10^(cb/(200*65536)) * V
0.1 % units
where pcunits = PercentUnits, percent = value between 0 and 100
pcunits = percent * 10 * 65536
percent = pcunits / (10 * 65536)
*/
class DLS {
	IOUtils::RIFFFile file;
public:
	int noInstr;
	int noSamples;
	struct Sample {
		unsigned int sampleRate;
		unsigned int length;
		const short *pointer;
	};

	struct RegionHeader {
		short rangeKeyLow, rangeKeyHigh;
		short rangeVelLow, rangeVelHigh;
		unsigned short options;
		unsigned short keyGroup;
	};

	struct RegionLoop {
		unsigned int sz;
		unsigned int loopType;
		unsigned int loopStart;
		unsigned int loopLength;
	};

	struct RegionMisc {
		short unityNote;
		short fineTune;
		long atten;
		long options;
		long cLoops;
	};

	struct RegionArticulation {
		int hej;	
	};
	
	struct Region {
		const RegionHeader *header;
		const RegionMisc *misc;
		const RegionLoop *loop;
		const RegionArticulation *art;
		int waveIndex;
		int fineTune;
	};

	struct Instrument {
#ifndef STREAMLINE
		char *name;
#endif
		int bank;
		int number;
		int nRegions;
		Region regions[100];
	};
	Sample samples[512];
	Instrument instruments[512];
	
	int GetRegionAndNoteOffset(int instrument, float fnote, float *noteOffset, float *sr);

	bool Open(const TCHAR *filename);
	void Close();	
};

}  // namespace

#endif